// AttributeBar.cpp : implementation file
//

#include "stdafx.h"
#include "Draw.h"
#include "AttributeBar.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAttributeBar dialog


CAttributeBar::CAttributeBar(CWnd* pParent /*=NULL*/)
	: CDialogBar(/*CAttributeBar::IDD, pParent*/)
{
	m_ForeColor = RGB(0, 0, 0);			// set the default color of foreground as black
	m_BackColor = RGB(255, 255, 255);	// set the default color of background as white
	m_bFilled = FALSE;					// set the default filling as false
	m_LineWidth = 1;					// set the default line width as 1 pixel
	m_LineStyle = PS_SOLID;				// set the default line style as PS_SOLID
	//m_CLineWidth = (CComboBox*)GetDlgItem(IDC_COMBO_LINEWIDTH);
}

void CAttributeBar::DoDataExchange(CDataExchange* pDX)
{
	CDialogBar::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAttributeBar)
	DDX_Control(pDX, IDC_LIST_FILLSTYLE, m_LFillStyle);
	DDX_Control(pDX, IDC_COMBO_LINEWIDTH, m_CLineWidth);
	DDX_Control(pDX, IDC_COMBO_LINESTYLE, m_CLineStyle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAttributeBar, CDialogBar)
	//{{AFX_MSG_MAP(CAttributeBar)
	ON_WM_LBUTTONDOWN()
	ON_WM_PAINT()
	ON_CBN_SELENDOK(IDC_COMBO_LINEWIDTH, OnSelendokComboLinewidth)
	ON_LBN_SELCHANGE(IDC_LIST_FILLSTYLE, OnSelchangeListFillstyle)
	ON_CBN_SELENDOK(IDC_COMBO_LINESTYLE, OnSelendokComboLinestyle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAttributeBar message handlers

BOOL CAttributeBar::Create(CWnd* pParentWnd,UINT nIDTemplate,UINT nStyle,UINT nID) 
{
	BOOL bRes = CDialogBar::Create(pParentWnd, nIDTemplate, nStyle, nID);
	InitDialogBar();		// initialize the dialog bar.
	return bRes;
}

BOOL CAttributeBar::InitDialogBar()
{
	// ensure the dialog bar could exchange data as control dialog.
	UpdateData(FALSE);

	static int nLineWidthes[] = {
		1, 2, 3, 4, 5, 6, 7
	};

	//
    // Create an 8-point MS Sans Serif font for the combo boxes.
	//
	CClientDC dc (this);
	m_font.CreatePointFont (80, _T ("MS Sans Serif"));
	CFont* pOldFont = dc.SelectObject (&m_font);

	TEXTMETRIC tm;
	dc.GetTextMetrics (&tm);
	int cxChar = tm.tmAveCharWidth;
	int cyChar = tm.tmHeight + tm.tmExternalLeading;

	dc.SelectObject (pOldFont);

	//
	// Add the line styles to the combo box m_CLineStyle
	//
	m_CLineStyle.SetFont(&m_font);

	m_CLineStyle.AddString (_T("_____"));
	m_CLineStyle.AddString (_T("_ _ _"));
	m_CLineStyle.AddString (_T("_ . _"));
	m_CLineStyle.AddString (_T("- - -"));	

	m_CLineStyle.SelectString(-1, _T("_____"));

	//
	// Add the Line widthes to the combo box m_CLineWidth
	//
	m_CLineWidth.SetFont (&m_font);
	CString string;
	int nCount = sizeof (nLineWidthes) / sizeof (int);
	for (int i = 0; i < nCount; i++) {
		string.Format (_T("%d"), nLineWidthes[i]);
		m_CLineWidth.AddString (string);
	}
	m_CLineWidth.SelectString(-1, _T("1"));

	//
	// Add the Icons into the IconListbox m_LFillStyle
	//
/*	HICON hIcon;
	// Load the house icon.
	
	hIcon = ::LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE (IDI_ICONNOTFILL));
	m_LFillStyle.AddIcon(hIcon);
	hIcon = ::LoadIcon (AfxGetResourceHandle (), MAKEINTRESOURCE (IDI_ICONFILL));
	m_LFillStyle.AddIcon(hIcon);
	*/
	m_LFillStyle.AddString(_T("UNFILL"));
	m_LFillStyle.AddString(_T(" F I L L"));
	m_LFillStyle.SetCurSel(0);

	return TRUE;
}

// when the user click down with left mouse button
void CAttributeBar::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// get the IDC_FORECOLOR static pic ctrl
	CWnd* pFore = GetDlgItem(IDC_FORECOLOR);
	ASSERT(pFore);
	// get the IDC_BACKCOLOR static pic ctrl
	CWnd* pBack = GetDlgItem(IDC_BACKCOLOR);
	ASSERT(pBack);

	// the rectangle of the forecolor ctrl
	CRect ForeRect;
	// the rectangle of the backcolor ctrl
	CRect BackRect;
	// get the rect of the forecolor rect
	pFore->GetClientRect(ForeRect);
	pFore->ClientToScreen(ForeRect);  
	ScreenToClient(ForeRect);
	// get the rect of the backcolor rect
	pBack->GetClientRect(BackRect);
	pBack->ClientToScreen(BackRect);  
	ScreenToClient(BackRect);

	CColorDialog dlg;		// the color dlg
	// set the attribute of the color dialog
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.Flags |= CC_FULLOPEN;
	dlg.m_cc.Flags |= CC_ANYCOLOR;
	// if the point that user click down is in the forerect
	if (ForeRect.PtInRect(point))
	{
		// set the default choice of color in the color dlg
		dlg.m_cc.rgbResult = m_ForeColor;
		// show the color dlg
		if (dlg.DoModal() == IDOK)
		{
			// get the user's choice and set it to forecolor
			m_ForeColor = dlg.GetColor();
			// refresh the ctrl
			Invalidate();
		}
	}
	// if the point that user click down is in the backrect
	else if (BackRect.PtInRect(point))
	{
		// set the default choice of color in the color dlg
		dlg.m_cc.rgbResult = m_BackColor;
		// show the color dlg
		if (dlg.DoModal() == IDOK)
		{
			// get the user's choice and set it to backcolor
			m_BackColor = dlg.GetColor();
			// refresh the ctrl
			Invalidate();
		}
	}
}

void CAttributeBar::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	// get the IDC_FORECOLOR static pic ctrl
	CWnd* pFore = GetDlgItem(IDC_FORECOLOR);
	ASSERT(pFore);
	// get the IDC_BACKCOLOR static pic ctrl
	CWnd* pBack = GetDlgItem(IDC_BACKCOLOR);
	ASSERT(pBack);

	CBrush Forebrush;
	CBrush Backbrush;
	// set the fore color brush
	Forebrush.CreateSolidBrush(m_ForeColor);
	// set the back color brush
	Backbrush.CreateSolidBrush(m_BackColor);

	// the rect of fore color static ctrl
	CRect ForeRect;
	// the rect of back color static ctrl
	CRect BackRect;

	// refresh the color shown on these ctrl
	pFore->GetClientRect(ForeRect);
	pFore->ClientToScreen(ForeRect);  
	pBack->GetClientRect(BackRect);
	pBack->ClientToScreen(BackRect);  
	ScreenToClient(ForeRect);
	ScreenToClient(BackRect);
	dc.FillRect(BackRect, &Backbrush);
	dc.FillRect(ForeRect, &Forebrush);
}

// when the user choose the line style
void CAttributeBar::OnSelendokComboLinestyle() 
{
	int nIndex = m_CLineStyle.GetCurSel();

	if (nIndex == 0)			// solid line
		m_LineStyle = PS_SOLID;
	if (nIndex == 1)			// dash line
		m_LineStyle = PS_DASH;
	if (nIndex == 2)			// dash dot line
		m_LineStyle = PS_DASHDOT;
	if (nIndex == 3)			// dot line
		m_LineStyle = PS_DOT;
}

// when the user choose the line width
void CAttributeBar::OnSelendokComboLinewidth() 
{
	TCHAR szWidth[8];
	// get the selected index
	int nIndex = m_CLineWidth.GetCurSel();
	// convert it to the width
	m_CLineWidth.GetLBText (nIndex, szWidth);

	m_LineWidth = atoi(szWidth);
}

void CAttributeBar::OnSelchangeListFillstyle() 
{
	// get the selected index of the fill style
	m_bFilled = m_LFillStyle.GetCurSel();
}

// when the user set the background color by click the item in menu
void CAttributeBar::OnMenuSetback() 
{
	CWnd* pBack = GetDlgItem(IDC_BACKCOLOR);
	ASSERT(pBack);

	CRect BackRect;	
	pBack->GetClientRect(BackRect);
	pBack->ClientToScreen(BackRect);  
	ScreenToClient(BackRect);

	// set the attribute of the color dlg
	CColorDialog dlg;
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.Flags |= CC_FULLOPEN;
	dlg.m_cc.Flags |= CC_ANYCOLOR;
	
	dlg.m_cc.rgbResult = m_BackColor;
	if (dlg.DoModal() == IDOK)
	{
		// get the user's choice of color, and set it to backcolor
		m_BackColor = dlg.GetColor();
		Invalidate();
	}
}

// when the user set the foreground color by click the item in menu
void CAttributeBar::OnMenuSetfore() 
{
	CWnd* pFore = GetDlgItem(IDC_FORECOLOR);
	ASSERT(pFore);

	CRect ForeRect;
	pFore->GetClientRect(ForeRect);
	pFore->ClientToScreen(ForeRect);  
	ScreenToClient(ForeRect);

	CColorDialog dlg;
	// set the attribute of the color dlg
	dlg.m_cc.Flags |= CC_RGBINIT;
	dlg.m_cc.Flags |= CC_FULLOPEN;
	dlg.m_cc.Flags |= CC_ANYCOLOR;

	dlg.m_cc.rgbResult = m_ForeColor;
	if (dlg.DoModal() == IDOK)
	{
		// get the user's choice and set it to fore color
		m_ForeColor = dlg.GetColor();
		Invalidate();
	}
}
